package com.zrrd.mcl.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.mcl.common.result.Result;
import com.zrrd.mcl.model.product.SkuInfo;
import com.zrrd.mcl.model.product.SpuImage;
import com.zrrd.mcl.model.product.SpuSaleAttr;
import com.zrrd.mcl.product.service.SkuInfoService;
import com.zrrd.mcl.rabbit.constant.MqConstant;
import com.zrrd.mcl.rabbit.service.RabbitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class SkuInfoController {
    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private RabbitService rabbitService;

    @GetMapping("list/{page}/{limit}")
    public Result<Page> page(@PathVariable Integer page, @PathVariable Integer limit, Long category3Id) {
        Page<SkuInfo> pageInfo = new Page<>(page, limit);
        skuInfoService.page(pageInfo,null);
        return Result.ok(pageInfo);
    }

    @GetMapping("/onSale/{skuId}")
    public Result<String> onSale(@PathVariable Long skuId) {
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(1);
        //发送MQ消息
        rabbitService.sendMessage(MqConstant.EXCHANGE_DIRECT_GOODS,MqConstant.ROUTING_GOODS_UPPER,skuId);
        return skuInfoService.updateById(skuInfo)?Result.ok("成功"):Result.fail("失败");
    }

    @GetMapping("/cancelSale/{skuId}")
    public Result<String> cancelSale(@PathVariable Long skuId) {
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(0);
        //发送MQ消息
        rabbitService.sendMessage(MqConstant.EXCHANGE_DIRECT_GOODS,MqConstant.ROUTING_GOODS_LOWER,skuId);
        return skuInfoService.updateById(skuInfo)?Result.ok("成功"):Result.fail("失败");
    }

    @GetMapping("/spuImageList/{spuId}")
    public Result<List<SpuImage>> spuImageList(@PathVariable("spuId") Long spuId) {
        List<SpuImage> spuImageList = skuInfoService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }

    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttr>> spuSaleAttrList(@PathVariable("spuId") Long spuId) {
        return Result.ok(skuInfoService.getSpuSaleAttrList(spuId));
    }

    @PostMapping("/saveSkuInfo")
    public Result<String> saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        skuInfoService.saveSkuInfo(skuInfo);
        return Result.ok("成功");
    }

}
